package com.echat.business.system.controller;

import com.echat.business.home.model.SysThirdUser;
import com.echat.business.home.model.SysUser;
import com.echat.business.home.service.ISysThirdUserService;
import com.echat.business.home.service.ISysUserService;
import com.echat.business.system.service.OauthService;
import com.echat.config.common.RequestHolder;
import com.echat.core.constant.CacheKey;
import com.echat.core.constant.response.ResponseData;
import com.echat.core.utils.RestClient;
import com.echat.core.utils.ValueUtils;
import com.echat.core.utils.redis.JedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class OauthController {

    @Autowired
    private RestClient restClient;

    @Autowired
    private ISysThirdUserService thirdUserService;

    @Autowired
    private ISysUserService sysUserService;


    @Autowired
    private OauthService oauthService;

    /**
     * 流程:
     * ①前端传入 thirdUser对象，将thirdUser入库并与sysUser建立关联关系
     * ②向oauth2资源服务器请求token，并将token和用户信息封装成sysUser放入redis缓存，
     * 【格式：键：accessToken（当前oauth2发放的令牌）  值：sysUser】
     * ③将sysUser对象返回前端
     *
     * @param thirdUser
     * @return
     */
    @RequestMapping(value = "/oauth/store", method = RequestMethod.POST)
    public ResponseData getToken(SysThirdUser thirdUser) {
        if (thirdUser != null) {
            Integer thirdId = thirdUser.getThirdId();
            String username = thirdUser.getLogin();
            String name = thirdUser.getName();
            String avatar = thirdUser.getAvatar();
            //ask 生成的第三方用户系统信息的  密码、盐、角色 暂时写死
            String password = ValueUtils.parseString(thirdId);
            String salt = "=fjksuk";
            String role = "general";
            SysUser sysUser;
            SysUser loginUser = RequestHolder.getCurrentUser();
            if (loginUser == null) {
                //thread中无登录用户
                int count = thirdUserService.selectCountByThirdId(thirdId);
                if (count == 0) {
                    //库内没有第三方用户
                    thirdUserService.insert(thirdUser);
                    sysUser = new SysUser(thirdId, username, name, password, salt, avatar, role);
                    sysUserService.insert(sysUser);
                } else {
                    //库内已有第三方用户信息
                    sysUser = sysUserService.getUserByThirdId(thirdId);
                }
                oauthService.getTokenFromUser(sysUser);
                System.out.println("current Thread" + Thread.currentThread());
                return ResponseData.success(sysUser);
            } else {
                //登录用户已存在
                return ResponseData.success(loginUser);
            }
        }
        return null;
    }

    @RequestMapping(value = "/oauth/loginOut", method = RequestMethod.POST)
    public ResponseData loginOut(SysUser sysUser) {
        if (sysUser == null) {
            return ResponseData.error("用户为空!");
        }
        String access_token = sysUser.getAccess_token();
        JedisUtil.del(CacheKey.USER_KEY + access_token);
        return ResponseData.success();
    }
}
